<template>
  <div class="chooseImageComponent" v-if="item">
    <span class="title">{{ item.title }}</span>
    <div class="right">
      <div class="buttonWrapper">
        <div class="uploadBtn" v-if="!value" @click="openChooseImg">
          <i class="el-icon-plus"></i>
        </div>
        <img
          v-if="value && String(value).indexOf('icon-') == -1"
          :src="value"
          alt
        />

        <span v-if="value" class="repaceImg" @click="updateChooseImg"
          >替换</span
        >

        <span v-if="value" class="el-icon-error delImg" @click="delImg"></span>
      </div>
      <span class="tips" v-if="item.tips" :title="item.tips">{{item.tips}}</span>
    </div>
  </div>
</template>
<script>
import { mapState } from "vuex";
import Bus from "@/utils/bus";
export default {
  name: "chooseImageComponent",
  data() {
    return {
      value: null,
      //为了防止页面初始化时，触发不必要的通知更新，此处设置标识符，初始化后2秒置为true
      needUpdate: false,
      cur: "", //当前选择图片的组件是哪个？
    };
  },
  created() {},
  props: ["item", "rightIndex", "catalogIndex", "ocIndex", "id"],
  components: {},
  mounted() {
    this.$nextTick(() => {
      this.init();
    });
    setTimeout(() => {
      this.needUpdate = true;
    }, 1000);
    let thisId = this.id;
    if(this.item.ename === "loginHeadImg"||this.item.ename === "invitationPageBgImg"||this.item.ename === "dataPageBgImg"||this.item.ename === "dataPageFloatBtnImg1"||this.item.ename === "dataPageFloatBtnImg2"||this.item.ename === "pageBgImg"||this.item.ename === "navBgImg"||this.item.ename === "moduleBgImg"||this.item.ename === "bgImg"||this.item.ename === "wrapperBgImg"||this.item.ename === "bottomBgImg") {
      thisId = this.item.id;
    }

    Bus.$off(`chooseImageAfter${thisId}`);
    Bus.$on(`chooseImageAfter${thisId}`, (params) => {
        this.value = params.url;
        this.updateParentHandle(params.url);
    });
  },
  methods: {
    //初始化
    init() {
      if (this.item) {
        this.value = null;
        this.$nextTick(() => {
          let oItem = JSON.parse(JSON.stringify(this.item));
          this.value = oItem.value || oItem.default;
        });
      }
    },
    //添加图片
    openChooseImg() {
      let thisId = this.id; 
      if(this.item.ename === "loginHeadImg"||this.item.ename === "invitationPageBgImg"||this.item.ename === "dataPageBgImg"||this.item.ename === "dataPageFloatBtnImg1"||this.item.ename === "dataPageFloatBtnImg2"||this.item.ename === "pageBgImg"||this.item.ename === "navBgImg"||this.item.ename === "bgImg"||this.item.ename === "wrapperBgImg"||this.item.ename === "moduleBgImg"||this.item.ename === "bottomBgImg") { 
        thisId = this.item.id;
      }
      Bus.$emit("openChooseImgGroup", {
        ename: "chooseImage",
        controlid: thisId,
      });
    },
    //替换图片
    updateChooseImg() {
      let thisId = this.id;
      if(this.item.ename === "loginHeadImg"||this.item.ename === "invitationPageBgImg"||this.item.ename === "dataPageBgImg"||this.item.ename === "dataPageFloatBtnImg1"||this.item.ename === "dataPageFloatBtnImg2"||this.item.ename === "pageBgImg"||this.item.ename === "navBgImg"||this.item.ename === "bgImg"||this.item.ename === "wrapperBgImg"||this.item.ename === "moduleBgImg"||this.item.ename === "bottomBgImg") {
        thisId = this.item.id;
      }
      Bus.$emit("openChooseImgGroup", {
        ename: "chooseImage",
        url: this.value,
        controlid: thisId,
      });
    },
    //删除图片
    delImg() {
      this.value = "";
      this.updateParentHandle("");
    },
    //更新到父组件
    updateParentHandle(value) {
      if (this.needUpdate) {
        let needUpdateMainChild = true;
        if (this.item.ename === "pageCover") {
          needUpdateMainChild = false;
        }
        let row = new Object();
        row.value = value;
        row.ename = this.item.ename;
        row.rightIndex = this.rightIndex;
        row.catalogIndex = this.catalogIndex;
        row.needUpdateMainChild = needUpdateMainChild;
        row.needUpdateMain = false;
        if (
          this.ocIndex !== null &&
          this.ocIndex !== undefined &&
          (this.ocIndex === 0 || this.ocIndex > 0)
        ) {
          row.ocIndex = this.ocIndex;
        }
        this.$emit("updateParent", JSON.stringify(row));
      }
    },
  },
  computed: mapState(["decorate"]),
  watch: {},
};
</script>
<style scoped lang="scss">
.chooseImageComponent {
  width: 100%;
  display: flex;
  align-items: center;
  .title {
    width: 100px;
    min-height: 32px;
    display: flex;
    justify-content: flex-end;
    font-size: 12px;
    align-items: center;
    padding-right: 10px;
  }
  .right {
    display: flex;
    align-items: center;
    flex: 1;
    width: calc(100% - 100px);
    .buttonWrapper {
      position: relative;
      display: flex;
      align-items: center;
      height: 40px;
      background-color: #fff;
      width: 40px;
      .content {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 100%;
        height: 100%;
      }
      .uploadBtn {
        width: 100%;
        height: 100%;
        display: flex;
        box-sizing: border-box;
        border-radius: 2px;
        justify-content: center;
        align-items: center;
        background-color: #fff;
        flex-direction: column;
        position: relative;
        cursor: pointer;
        border: 1px solid #e9edef;
        i {
          color: #0079fe;
          font-weight: bold;
          font-size: 14px;
          margin-top: 5px;
          margin-bottom: 5px;
        }
      }
      img {
        width: 100%;
        height: 100%;
        display: flex;
      }
      .repaceImg,
      .delImg {
        position: absolute;
        z-index: 3;
        opacity: 0.5;
        display: none;
        cursor: pointer;
        &:hover {
          cursor: pointer;
        }
      }
      .repaceImg {
        left: 0;
        bottom: 0px;
        width: 100%;
        height: 20px;
        font-size: 12px;
        background-color: rgba(0, 0, 0, 0.8);
        color: #fff;
        justify-content: center;
      }
      .delImg {
        right: -4px;
        top: -4px;
      }
      &:hover {
        .repaceImg,
        .delImg {
          display: flex;
        }
      }
    }
    .tips{font-size:12px;color:#999;margin-left:10px;text-overflow: ellipsis;white-space:nowrap;overflow:hidden;
}
  }
}
</style>